package at.ac.tuwien.sepm.s2011.e0208329.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import at.ac.tuwien.sepm.s2011.e0208329.dao.DAOFactoryImpl;
import at.ac.tuwien.sepm.s2011.e0208329.dao.DienstleistungDAO;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAOImpl;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Rechnung;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyBillServiceImpl;

@SuppressWarnings("serial")
public class CreateBillUI extends JFrame {
	private JTextField[] fields;
	private String[] labels;
	private JPanel labelPanel;
	private JPanel fieldPanel;
	private static final Logger log = Logger.getLogger(CreateBillUI.class);
	public CreateBillUI(String name) {
		super(name);
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		//services = serDAO.getAllServices();
		//serNum = services.size();		
	    labelPanel = new JPanel(new GridLayout(2, 1));
	    fieldPanel = new JPanel(new GridLayout(2, 1));
		fields = new JTextField[2];
		labels = new String[2];
		labels[0] = "Kundenname";
		labels[1] = "Addresse";
//		int labIndex = 2;
//		for (Dienstleistung ser: services) {
//			labels[labIndex] = ser.getName();
//			labIndex++;
//		}
		
		for (int i = 0; i < labels.length; i++) { 
			fields[i] = new JTextField();
			fields[i].setColumns(25);
			JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
			lab.setLabelFor(fields[i]);
			labelPanel.add(lab);
			JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
			p.add(fields[i]);
			fieldPanel.add(p);
		}
		
		JButton submit = new JButton("Neue Rechnung speichern");
	    submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
				Rechnung bill;
				boolean ok = false;
				try {										
					bill = new Rechnung(fields[0].getText(), fields[1].getText()); // read the entries
					if (bill != null) {
						//DAOFactoryImpl.getDAOFactory().getBillDAO().insertBill(bill);
						MyBillServiceImpl.getInstance().createBill(bill); //do the insert bill
						ok = true;
					}
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null,
							"Invalid data format !!!", "Data input error",
							JOptionPane.ERROR_MESSAGE);
				} catch (SQLException e2) {
					log.error(e2.getMessage());
					JOptionPane.showMessageDialog(null,
							"Please see log file for more detail !", "Neue Rechnung speichern: SQL error",
							JOptionPane.ERROR_MESSAGE);
				}
	        	if (ok) {
	        		for (int i = 0; i < fields.length; i++) {
	        			fields[i].setText("");
	        		}
	        	}
	        }
	      });
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(submit);
		this.getContentPane().add(labelPanel, BorderLayout.WEST);
	    this.getContentPane().add(fieldPanel, BorderLayout.CENTER);
	    this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
	    this.pack();
	}
	

}
